﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using HCY.CTP.DBShangZheng;
using IFServerDLL.DB;
using IFServerDLL;
using System.ComponentModel;

namespace IFClientDLL
{
    public class MarketDataManager_DB : IMarketDataServer, IPlay
    {
        private ITryIF _tryif;
        private BackgroundWorker _backGroundWork;
        public MarketDataManager_DB(ITryIF tryif, BackgroundWorker backgroundWork)
        {
            _backGroundWork = backgroundWork;
            _tryif = tryif;
        }
        private object _syncObject = new object();
        public void Start()
        {
            _backGroundWork.DoWork += new DoWorkEventHandler(_backGroundWork_DoWork);
            _backGroundWork.WorkerReportsProgress = true;
            _backGroundWork.ProgressChanged += new ProgressChangedEventHandler(_backGroundWork_ProgressChanged);
            _backGroundWork.RunWorkerAsync();
        }
        MyData_IF data_IF = null;
        MyData_RedGreen data_RedGreen = null;
        MyData_DaPan data_DaPan = null;
        void _backGroundWork_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 1)
            {
                if (OnReceiveIFData != null)
                {
                    DBTableMarketData d = e.UserState as DBTableMarketData;
                    data_IF = new MyData_IF(_tryif.DataInfo, d, data_IF);
                    OnReceiveIFData(data_IF);
                }
                if (OnReceiveDBData != null)
                {
                    DBTableMarketData d = e.UserState as DBTableMarketData;
                    MyDBData dbd = new MyDBData(_tryif.DataInfo, d);
                    OnReceiveDBData(dbd);
                }
            }
            else if (e.ProgressPercentage == 2)
            {
                if (OnReceiveMarketData != null)
                {
                    DBTableShangZheng d = e.UserState as DBTableShangZheng;
                    data_DaPan = new MyData_DaPan(d, data_DaPan);
                    OnReceiveMarketData(data_DaPan);
                }
            }
            else if (e.ProgressPercentage == 3)
            {
                if (OnReceiveRedGreenBarData != null)
                {
                    DBTableDzhRedGreenbar d = e.UserState as DBTableDzhRedGreenbar;
                    data_RedGreen = new MyData_RedGreen(d, data_RedGreen);
                    OnReceiveRedGreenBarData(data_RedGreen);
                }
            }
            else
            {

            }
        }

        void _backGroundWork_DoWork(object sender, DoWorkEventArgs e)
        {


            string errMsg;
            var listIF = MyDBHelper.GetIFByData(_tryif.DataInfo, out errMsg);
            Dictionary<int, DBTableMarketData> dictif = new Dictionary<int, DBTableMarketData>();
            foreach (var x in listIF.OrderBy(p => p.成交量))
            {
                int k = GlobalStatic.GetIntByTime(x.报单时间);
                if (k < 0)
                    continue;
                if (dictif.ContainsKey(k) == false)
                {
                    dictif.Add(k, x);
                }
                else
                {
                    for (int i = 1; i < 4; i++)
                    {
                        if (dictif.ContainsKey(k + i) == false)
                        {
                            dictif.Add(k + i, x);
                            x.报单时间 = x.报单时间.AddMilliseconds((i) * 200);
                            break;
                        }
                    }
                }
            }
            var listMarket = MyDBHelper.GetMarket(_tryif.DataInfo.日期);
            Dictionary<int, DBTableShangZheng> dictMarket = new Dictionary<int, DBTableShangZheng>();
            foreach (var x in listMarket.OrderBy(p => p.时间))
            {
                if (x.服务器时间 < 0)
                    continue;
                if (dictMarket.ContainsKey(x.服务器时间) == false)
                {
                    dictMarket.Add(x.服务器时间, x);
                    x.时间 = x.时间.AddMilliseconds(-x.时间.Millisecond);
                }
                else
                {
                    for (int i = 1; i < 4; i++)
                    {
                        if (dictMarket.ContainsKey(x.服务器时间 + i) == false)
                        {
                            dictMarket.Add(x.服务器时间 + i, x);
                            x.时间 = x.时间.AddMilliseconds((i) * 200);
                            break;
                        }
                    }
                }
            }
            var listRedGreen = MyDBHelper.GetRedGreenBar(_tryif.DataInfo.日期);
            Dictionary<int, DBTableDzhRedGreenbar> dictRedGreen = new Dictionary<int, DBTableDzhRedGreenbar>();
            foreach (var x in listRedGreen.OrderBy(p => p.时间))
            {
                if (x.服务器时间 < 0)
                    continue;
                if (dictRedGreen.ContainsKey(x.服务器时间) == false)
                {
                    dictRedGreen.Add(x.服务器时间, x);
                    x.时间 = x.时间.AddMilliseconds(-x.时间.Millisecond);
                }
                else
                {
                    for (int i = 1; i < 4; i++)
                    {
                        if (dictRedGreen.ContainsKey(x.服务器时间 + i) == false)
                        {
                            dictRedGreen.Add(x.服务器时间 + i, x);
                            x.时间 = x.时间.AddMilliseconds((i) * 200);
                            break;
                        }
                    }
                }
            }
            if (errMsg != string.Empty)
            {
                _tryif.AddMessage(new IFServerDLL.Model.MessageBase(errMsg));
            }

            for (int i = 0; i < 64812; i++)
            {
                if (_isPause)
                {
                    while (true)
                    {
                        Thread.Sleep(100);
                        if (_isPause == false)
                        {
                            break;
                        }
                    }
                }
                if (dictif.ContainsKey(i))
                {
                    _backGroundWork.ReportProgress(1, dictif[i]);

                }
                if (dictMarket.ContainsKey(i))
                {
                    _backGroundWork.ReportProgress(2, dictMarket[i]);
                }
                if (dictRedGreen.ContainsKey(i))
                {
                    _backGroundWork.ReportProgress(3, dictRedGreen[i]);
                }
                if (_interval < 50)
                {
                    Thread.Sleep(50);
                }
                else
                {
                    Thread.Sleep(_interval);
                }
            }

        }
        public event Action<MyData_IF> OnReceiveIFData;

        private bool _isPause = true;
        private int _interval = 0;
        public void Play()
        {
            _isPause = false;
        }

        public void Pause()
        {
            _isPause = true;
        }

        public void SetInterval(int interval)
        {
            _interval = interval;
        }





        public bool IsPlaying
        {
            get { return !_isPause; }
        }








        public event Action<MyData_RedGreen> OnReceiveRedGreenBarData;

        public event Action<MyData_DaPan> OnReceiveMarketData;


        public event Action<MyDBData> OnReceiveDBData;
    }
}
